<template>
  <div class="login-container">
    <!-- 背景视频 -->
    <div class="shadow" />
    <video
      src="https://slwl-admin.itheima.net/static/media/video.3cf57b17.mp4"
      autoplay
      loop
      muted
      style="width: 100%; height: 100%; object-fit: fill"
    />
    <!-- 登录窗口 -->
    <el-form ref="login" class="login-form" :model="loginForm" :rules="rules" autocomplete="off" @submit.native="login">
      <!-- logo -->
      <div class="title-container">
        <img
          src="https://slwl-admin.itheima.net/static/img/logo1.33ef7fa0.png"
          alt=""
        >
      </div>
      <!-- 表单 -->
      <span>
        <el-form-item prop="account">
          <el-input v-model="loginForm.account" autocomplete="off" placeholder="账号" />
          <img
            class="icon-img"
            src=""
            alt=""
          >
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" autocomplete="off" type="password" placeholder="密码" show-password />
          <img
            class="icon-img"
            src=""
            alt=""
          >
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="loginForm.code" autocomplete="off" placeholder="验证码" />
          <img
            class="icon-img"
            src=""
            alt=""
          >
          <img class="code-image" :src="`https://slwl-api.itheima.net/manager/captcha?key=${loginForm.key}`" alt="codeImage" @click="loginForm.key = generateRandomString()">
        </el-form-item>
        <el-button class="login-button" native-type="submit">
          登录
        </el-button>
      </span>
    </el-form>
    <!-- 底部版权号 -->
    <div class="login-footer">
      <div
        style="
          font-size: 14px;
          color: white;
          text-align: center;
          position: absolute;
          bottom: 0px;
          left: 0%;
          width: 100%;
          opacity: 0.9;
        "
      >
        江苏传智播客教育科技股份有限公司
        <span
          style="margin-left: 20px"
        >版权所有Copyright 2006-2022 All Rights Reserved</span>
        <span style="margin-left: 20px">苏ICP备16007882号-11</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        // 判断是开发环境还是生产环境，自动选择输入框是否写死
        account: process.env.NODE_ENV === 'development' ? 'shenlingadmin' : '',
        password: process.env.NODE_ENV === 'development' ? '123456' : '',
        code: '',
        key: '',
        bindAccount: '',
        bindPassword: '',
        signAccount: '',
        signPassword: '',
        tenant: '0000'
      },
      rules: {
        account: [{ required: true, message: '不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loginForm.key = this.generateRandomString()
  },
  methods: {
    generateRandomString() {
      let result = ''
      const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
      const charactersLength = characters.length
      for (let i = 0; i < 37; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength))
      }
      return result
    },
    login() {
      this.$refs.login.validate(async(isOK) => {
        if (isOK) {
          await this.$store.dispatch('user/login', this.loginForm)
          this.$message.success('登陆成功')
          this.$router.push('/')
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-container {
  background-size: 100% 100%;
  height: 100vh;
  overflow-y: hidden;
  position: relative;
  .shadow {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0.3;
    background: #1a1c41;
  }
  .login-form {
    position: absolute;
    top: 45%;
    left: 45%;
    margin: -180px 0 0 -160px;
    width: 450px;
    height: 467px;
    padding: 36px 40px;
    background: #fff;
    border-radius: 10px;
  }
  .title-container {
    position: relative;
    margin-bottom: 28px;
    text-align: center;
    img {
      height: 54px;
    }
  }
  .login-footer {
    position: fixed;
    bottom: 20px;
    width: 100%;
    text-align: center;
    color: #fff;
    font-size: 0.85rem;
    line-height: 1rem;
    height: 1rem;
  }
  .el-form-item {
    border-radius: 5px;
    color: #454545;
    -webkit-transition: all 0.3s;
    transition: all 0.3s;
    margin-bottom: 22px;
  }
  .el-input {
    position: relative;
    font-size: 14px;
    display: inline-block;
    width: 100%;
  }
  .el-form-item__content .el-input__inner {
    padding-left: 48px;
    height: 50px;
  }
  .icon-img {
    width: 21px;
    height: 21px;
    position: absolute;
    left: 14px;
    top: 13px;
  }
  .code-image {
    display: inline-block;
    vertical-align: top;
    width: 71px;
    height: 32px;
    cursor: pointer;
    margin-left: 10px;
    position: absolute;
    right: 10px;
    top: 9px;
  }
  .login-button {
    width: 100%;
    height: 54px;
    margin-bottom: 14px;
    margin-top: 22px;
    background-color: #e15536;
    border: 0;
    font-size: 18px;
    color: #fff;
    font-weight: 700;
  }
}
</style>
